//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.cancel.asyncImage;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Rect;
import android.graphics.BitmapFactory.Options;
import android.graphics.drawable.BitmapDrawable;
import android.widget.ImageView;
import com.tool.utils.DataUtil;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Collections;
import java.util.Map;
import java.util.WeakHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class CImageLoader {
    public static final String DefaultNULL = "www.null.image";
    MemoryCache memoryCache = new MemoryCache();
    FileCache fileCache;
    int REQUIRED_SIZE = 120;
    int defaultImage = 0;
    private Map<ImageView, String> imageViews = Collections.synchronizedMap(new WeakHashMap());
    ExecutorService executorService;

    public CImageLoader(Context context, String pathName, int resid) {
        DataUtil.dex();
        this.fileCache = new FileCache(context, pathName);
        this.executorService = Executors.newFixedThreadPool(5);
        this.defaultImage = resid;
        this.memoryCache.put("www.null.image", DataUtil.drawableToBitmap(context.getResources().getDrawable(resid)));
    }

    public void setREQUIRED_SIZE(int rEQUIRED_SIZE) {
        this.REQUIRED_SIZE = rEQUIRED_SIZE;
    }

    public void DisplayImage(String url, ImageView imageView) {
        if(url != null && url.length() > 0) {
            this.imageViews.put(imageView, url);
            Bitmap bitmap = this.memoryCache.get(url);
            if(bitmap != null) {
                imageView.setBackgroundDrawable(new BitmapDrawable(bitmap));
            } else {
                this.queuePhoto(url, imageView);
            }
        }

    }

    private void queuePhoto(String url, ImageView imageView) {
        CImageLoader.PhotoToLoad p = new CImageLoader.PhotoToLoad(url, imageView);
        this.executorService.submit(new CImageLoader.PhotosLoader(p));
    }

    private Bitmap getBitmap(String url) {
        File f = this.fileCache.getFile(url);
        Bitmap b = this.decodeFile(f);
        if(b != null) {
            return b;
        } else {
            try {
                Bitmap bitmap = null;
                URL imageUrl = new URL(url);
                HttpURLConnection conn = (HttpURLConnection)imageUrl.openConnection();
                conn.setConnectTimeout(30000);
                conn.setReadTimeout(30000);
                conn.setInstanceFollowRedirects(true);
                InputStream is = conn.getInputStream();
                OutputStream os = new FileOutputStream(f);
                CopyStream(is, os);
                os.close();
                bitmap = this.decodeFile(f);
                return bitmap;
            } catch (Exception var9) {
                var9.printStackTrace();
                return null;
            }
        }
    }

    private Bitmap decodeFile(File f) {
        try {
            Options o = new Options();
            o.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(new FileInputStream(f), (Rect)null, o);
            int width_tmp = o.outWidth;
            int height_tmp = o.outHeight;

            int scale;
            for(scale = 1; width_tmp / 2 >= this.REQUIRED_SIZE && height_tmp / 2 >= this.REQUIRED_SIZE; scale *= 2) {
                width_tmp /= 2;
                height_tmp /= 2;
            }

            Options o2 = new Options();
            o2.inSampleSize = scale;
            return BitmapFactory.decodeStream(new FileInputStream(f), (Rect)null, o2);
        } catch (FileNotFoundException var7) {
            return null;
        }
    }

    boolean imageViewReused(CImageLoader.PhotoToLoad photoToLoad) {
        String tag = (String)this.imageViews.get(photoToLoad.imageView);
        return tag == null || !tag.equals(photoToLoad.url);
    }

    public void clearCache() {
        this.memoryCache.clear();
        this.fileCache.clear();
    }

    public static void CopyStream(InputStream is, OutputStream os) {
        boolean var2 = true;

        try {
            byte[] bytes = new byte[1024];

            while(true) {
                int count = is.read(bytes, 0, 1024);
                if(count == -1) {
                    break;
                }

                os.write(bytes, 0, count);
            }
        } catch (Exception var5) {
            ;
        }

    }

    class BitmapDisplayer implements Runnable {
        Bitmap bitmap;
        CImageLoader.PhotoToLoad photoToLoad;

        public BitmapDisplayer(Bitmap b, CImageLoader.PhotoToLoad p) {
            this.bitmap = b;
            this.photoToLoad = p;
        }

        public void run() {
            if(!CImageLoader.this.imageViewReused(this.photoToLoad)) {
                if(this.bitmap != null) {
                    this.photoToLoad.imageView.setBackgroundDrawable(new BitmapDrawable(this.bitmap));
                }

            }
        }
    }

    private class PhotoToLoad {
        public String url;
        public ImageView imageView;

        public PhotoToLoad(String u, ImageView i) {
            this.url = u;
            this.imageView = i;
        }
    }

    class PhotosLoader implements Runnable {
        CImageLoader.PhotoToLoad photoToLoad;

        PhotosLoader(CImageLoader.PhotoToLoad photoToLoad) {
            this.photoToLoad = photoToLoad;
        }

        public void run() {
            if(!CImageLoader.this.imageViewReused(this.photoToLoad)) {
                Bitmap bmp = CImageLoader.this.getBitmap(this.photoToLoad.url);
                CImageLoader.this.memoryCache.put(this.photoToLoad.url, bmp);
                if(!CImageLoader.this.imageViewReused(this.photoToLoad)) {
                    CImageLoader.BitmapDisplayer bd = CImageLoader.this.new BitmapDisplayer(bmp, this.photoToLoad);
                    Activity a = (Activity)this.photoToLoad.imageView.getContext();
                    a.runOnUiThread(bd);
                }
            }
        }
    }
}
